package org.jeecg.modules.tdm.utils;

import cn.hutool.core.codec.Base64;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jeecg.modules.tdm.entity.TestFiles;
import org.jeecg.modules.tdm.entity.TestMain;
import org.jeecg.modules.tdm.entity.TestValue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestBeanUtils {


    /**
     * xml参数解析成list
     *
     * @param xmlParam
     * @return
     * @throws Throwable
     */
    public static List xmlToBeanList(String xmlParam) {
        String xmlStr = xmlParam.replace("&", "&amp;");
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlStr);
        } catch (
                DocumentException e) {
            e.printStackTrace();
        }

        List recordMapList = new ArrayList();

        XStream xStream = new XStream(new XppDriver(new NoNameCoder()));
        xStream.processAnnotations(TestMain.class);
        xStream.processAnnotations(TestFiles.class);
        xStream.processAnnotations(TestValue.class);
        xStream.autodetectAnnotations(true);
        xStream.setClassLoader(TestMain.class.getClassLoader());
        xStream.setupDefaultSecurity(xStream);
        xStream.allowTypes(new Class[]
                {
                        TestMain.class,
                        TestFiles.class,
                        TestValue.class
                });
        //request
        Element root = doc.getRootElement();
        //list
        List listList = root.elements();
        //record
        for (int i = 0; i < listList.size(); i++) {
            Map<String, Object> recordMap = new HashMap<String, Object>();
            //
            Element listEle = (Element) listList.get(i);
            //String record = listEle.getName();
            //recold内三个entityList组成的List
            List recordList = listEle.elements();
            for (int j = 0; j < recordList.size(); j++) {
                Element recordEle = (Element) recordList.get(j);
                if (recordEle.getName().equals("ITEM")) {
                    //解析主表testmain
                    TestMain testMain = (TestMain) xStream.fromXML(recordEle.asXML());
                    //resultList.add(testMain);
                    recordMap.put("ITEM", testMain);
                } else if (recordEle.getName().equals("FILES")) {
                    //解析文件子表testfiles
                    List<TestFiles> fileList = new ArrayList<>();
                    List fileEleList = recordEle.elements();
                    for (int m = 0; m < fileEleList.size(); m++) {
                        Element fileEle = (Element) fileEleList.get(m);
                        TestFiles testFile = (TestFiles) xStream.fromXML(fileEle.asXML());
                        fileList.add(testFile);
                    }
                    //resultList.add(fileList);
                    recordMap.put("FILES", fileList);
                } else if (recordEle.getName().equals("VALUES")) {
                    //解析结果子表testvalue
                    List<TestValue> valueList = new ArrayList<>();
                    List valueElelist = recordEle.elements();
                    for (int n = 0; n < valueElelist.size(); n++) {
                        Element valueEle = (Element) valueElelist.get(n);
                        TestValue testValue = (TestValue) xStream.fromXML(valueEle.asXML());
                        valueList.add(testValue);
                    }
                    //resultList.add(valueList);
                    recordMap.put("VALUES", valueList);
                }

            }
            recordMapList.add(recordMap);
        }
        return recordMapList;
    }


    public static String beanToXml(Object obj) {
        XStream xStream = new XStream();
        xStream.processAnnotations(DataGridView.class);
        return xStream.toXML(obj);
    }

    /**
     * 用反射给实体类赋值
     *
     * @param <T>
     * @param t
     * @return
     */
    public static <T> T byField(T t) {

        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field2 : fields) {
            try {
                field2.setAccessible(true);
                field2.set(t, "1");
                //System.out.println("成员属性:"+field2.getName()+" 成员属性修饰符: "+field2.getModifiers()+" 成员属性值: "+field2.get(t));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return t;
    }

    private static String getFileByteString(File file) {
        String encodedFileString = "";
        try {
            InputStream in = new FileInputStream(file);
            // 取得文件大小
            long length = file.length();
            // 根据大小创建字节数组
            byte[] bytes = new byte[(int) length];
            // 读取文件内容到字节数组
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
            // 读取完毕的校验
            if (offset < bytes.length) {
                throw new IOException("不能完全讀取文件：" + file.getName());
            }
            in.close();
            encodedFileString = Base64.encode(bytes);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

        }
        return encodedFileString;
    }

}
